package com.xuzz.study.io.netty.rpc.provicer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author zhize.xu
 * @version 1.0
 * @ClassName:Server
 * @Description:
 * @date 2018/11/27 14:45
 */
public class NettyServer {
    public static void startService()
    {
        try
        {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
            serverBootstrap.group(eventLoopGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>()
                    {
                        @Override
                        protected void initChannel(SocketChannel channel) throws Exception {
                            channel.pipeline().addLast(new StringDecoder());
                            channel.pipeline().addLast(new StringEncoder());
                            channel.pipeline().addLast(new ServiceHandler());
                        }
                    });
            serverBootstrap.bind("127.0.0.1",8080).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
